package com.sita.tboard.ui.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sita.bike.R;
import com.sita.bike.support.GlobalContext;

import java.util.ArrayList;
import java.util.List;

import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.model.Viewport;
import lecho.lib.hellocharts.view.LineChartView;

public class RpmLineChartFragment extends Fragment {
    private static final int WHITE = GlobalContext.getGlobalContext().getResources().getColor(R.color.white);
    private static RpmLineChartFragment instance;
    private LineChartView chart;
    private LineChartData data;
    private int numberOfPoints = 12;
    private boolean hasAxes = true;//时否有坐标轴

    public RpmLineChartFragment() {
    }

    public static RpmLineChartFragment getInstance() {
        instance = new RpmLineChartFragment();
        return instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        setHasOptionsMenu(true);
        View rootView = inflater.inflate(R.layout.fragment_line_chart, container, false);
        chart = (LineChartView) rootView.findViewById(R.id.chart);
        // Generate some randome values.
        generateData();
        // Disable viewpirt recalculations, see toggleCubic() method for more info.
        chart.setViewportCalculationEnabled(false);
        //设置坐标轴的范围
        initViewport();
        return rootView;
    }

    //设置x，y的范围
    private void initViewport() {
        // Reset viewport height range to (0,100)
        final Viewport v = new Viewport(chart.getMaximumViewport());
        v.bottom = 0;
        v.top = 100;
//        v.left = 0;
//        v.right = numberOfPoints - 1;
        chart.setMaximumViewport(v);
        chart.setCurrentViewport(v);
    }
    private void generateData() {

        List<Line> lines = new ArrayList<>();
        //速度的值应该传到这个values里面
        List<PointValue> values = new ArrayList<>();
        for (int j = 0; j < 20; ++j) {
            values.add(new PointValue(j, 0));
        }
        Line line = new Line(values);
        line.setColor(WHITE);
        line.setShape(ValueShape.CIRCLE);
        line.setCubic(false);
        line.setFilled(false);
        line.setHasLabels(false);
        line.setHasLines(true);
        line.setHasPoints(true);
        line.setPointColor(WHITE);
        line.setPointRadius(3);
        line.setStrokeWidth(1);
        lines.add(line);

        data = new LineChartData(lines);

        if (hasAxes) {
            Axis axisX = new Axis();
            //y轴没有横线
            Axis axisY = new Axis().setHasLines(false);
            axisY.setLineColor(WHITE);
            axisX.setLineColor(WHITE);
            axisX.setMaxLabelChars(21);
            data.setAxisXBottom(axisX);
            data.setAxisYLeft(axisY);
        } else {
            data.setAxisXBottom(null);
            data.setAxisYLeft(null);
        }

        data.setBaseValue(Float.NEGATIVE_INFINITY);
        chart.setLineChartData(data);

    }
}

